"use strict"
export const getRandomColor = function(){

    const getRandom = function(color){
        return (color +=  '0123456789abcdef'[Math.floor(Math.random()*16)])
        && (color.length === 6) ?  color : getRandom(color);
    }

    return  '#' + (getRandom)('');

}

export const toJson = function (dataName,data) {
    const filename = `${dataName}.json`;
    const json = JSON.stringify(data);
    var blob = new Blob([json], { type: "text/json" }),
        e = document.createEvent("MouseEvents"),
        a = document.createElement("a");
    a.download = filename;
    a.href = window.URL.createObjectURL(blob);
    a.dataset.downloadurl = ["text/json", a.download, a.href].join(":");
    e.initMouseEvent(
        "click",
        true,
        false,
        window,
        0,
        0,
        0,
        0,
        0,
        false,
        false,
        false,
        false,
        0,
        null
    );
    a.dispatchEvent(e);
}

const shapefile = require('shapefile');
export const shapeToMap = function(file){
    // 读取 Shapefile 文件
    shapefile.read(file).then(geojson => {
        // 处理 GeoJSON 数据
        console.log(geojson);
        console.log("-------------------------")
        // 遍历每个特征
        geojson.features.forEach(feature => {
            console.log('Feature geometry.type:', feature.geometry);
            console.log('Feature geometry.type:', feature.geometry.type);
            console.log('Feature geometry.coordinates:', feature.geometry.coordinates);
            console.log('Feature properties:', feature.properties);
        });
    })
        .catch(error => {
            console.error('Error reading shapefile:', error);
        });

}

export const getUUID =function() {
    // 检查是否支持 crypto 模块
    if (typeof crypto === 'undefined' || typeof crypto.getRandomValues!== 'function') {
        throw new Error('浏览器不支持：Crypto module is not supported in this environment.');
    }
    // 创建一个包含 16 个元素的 Uint8Array 数组
    const array = new Uint8Array(16);
    // 生成随机字节填充数组
    crypto.getRandomValues(array);

    // 设置版本号为 4（随机生成的 UUID）
    array[6] = (array[6] & 0x0f) | 0x40;
    // 设置变体号为 RFC4122
    array[8] = (array[8] & 0x3f) | 0x80;

    // 将字节数组转换为 UUID 字符串
    const toHex = (num) => {
        return ('00' + num.toString(16)).slice(-2);
    };
    return [
        toHex(array[0]) + toHex(array[1]) + toHex(array[2]) + toHex(array[3]),
        toHex(array[4]) + toHex(array[5]),
        toHex(array[6]) + toHex(array[7]),
        toHex(array[8]) + toHex(array[9]),
        toHex(array[10]) + toHex(array[11]) + toHex(array[12]) + toHex(array[13]) + toHex(array[14]) + toHex(array[15])
    ].join('-');
}
